import { createRouter, RouterMount, runtimeQuit } from 'uni-simple-router';
import Vue from 'vue';

const whiteList = ['blank', 'login', 'sms-login', 'register', 'forget'];

const router = createRouter({
  platform: process.env.VUE_APP_PLATFORM,
  routes: [...ROUTES], // ROUTES 这个名称是webpack 中配置的名称 => vue.config.js
  //全局路由前置守卫
  routerBeforeEach: (to, from, next) => {
    //白名单
    //console.log(to, from);
    if (!whiteList.includes(to.name)) {
      const token = uni.getStorageSync(
        'tokenSer' + process.env.VUE_APP_PLATFORMID
      );
      // if (!token) {
      //   return next({
      //     name: 'login',
      //     NAVTYPE: 'replaceAll',
      //   });
      // }
    } else if (to.name === 'blank') {
      return next({
        name: 'index',
        NAVTYPE: 'replaceAll',
      });
    }
    //白名单
    next();
  },
  routerErrorEach: ({ type }) => {
    // #ifdef APP-PLUS
    if (type === 3) {
      router.$lockStatus = false;
      runtimeQuit();
    }
    // #endif
  },
});

Vue.use(router);

export { router, RouterMount };
